import 'dart:async';

import 'package:flutter/material.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBCommonToast/YBCommonToastPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBLoginInfoModule/YBLoginInfoApi.dart';

class YBSettingNewPhonePage extends StatefulWidget {

  final String phone;

  const YBSettingNewPhonePage({Key key, this.phone}) : super(key: key);


  @override
  _YBSettingNewPhonePageState createState() => _YBSettingNewPhonePageState(phoneStr: phone);
}

class _YBSettingNewPhonePageState extends State<YBSettingNewPhonePage> {

  final String phoneStr;

  _YBSettingNewPhonePageState({this.phoneStr});

  TextEditingController _editingController = TextEditingController();
  TextEditingController _phoneEditingController = TextEditingController();
  Timer _timer;
  int _timeCount = 60;
  String _autoText;
  String msgTokenStr;

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Scaffold(
         appBar: AppBar(
            elevation: 0,
            title: Text(
              YBChineseText.mineNewPhoneTitle,
              style: YBTextStyle.TextStyle30313320Medium,
            ),
         ),
         body: SingleChildScrollView(
           child: Padding(
             padding:EdgeInsets.fromLTRB(YBTextStyle.adapterSize20, YBTextStyle.adapterSize30, YBTextStyle.adapterSize20,0),
             child: Container(
               color: YBColor.white,
               child: Column(
                 mainAxisAlignment: MainAxisAlignment.start,
                 crossAxisAlignment: CrossAxisAlignment.stretch,
                 children: [
                   Container(
                     alignment: Alignment.center,
                     child: Text(YBChineseText.mineCurrentBindingPhoneTitle,style: YBTextStyle.TextStyle60626611,),
                   ),
                   Container(
                     height: YBTextStyle.adapterSize5,
                   ),
                   Container(
                     alignment: Alignment.center,
                     child: Text(phoneStr??"",style: YBTextStyle.TextStyle18181820Medium,),
                   ),
                   Container(
                     height: YBTextStyle.adapterSize40,
                   ),
                   Container(
                     alignment: Alignment.center,
                     child: Text(YBChineseText.mineYanZhengShengFengTitle,style: YBTextStyle.TextStyle1B1C1F18Medium,),
                   ),
                   Container(
                     height: YBTextStyle.adapterSize30,
                   ),
                   Container(
                     height: YBTextStyle.adapterSize50,
                     color: YBColor.white,
                     child: Row(
                       children: [
                         Container(
                           child: Image.asset(YBImageStyle.getImagePath('login_phone')),
                         ),
                         Container(
                           width: YBTextStyle.adapterSize8,
                         ),
                         Expanded(
                           child: TextField(
                             decoration: InputDecoration(
                               hintText: YBChineseText.mineInputTelPhoneTitle,
                               border: InputBorder.none,
                             ),
                             controller: _phoneEditingController,
                           ),
                         ),
                       ],
                     ),
                   ),
                   Container(
                     color: YBColor.colorF5F5F7,
                     height: 1,
                   ),
                   Container(
                     height: YBTextStyle.adapterSize50,
                     child: Row(
                       children: [
                         Container(
                           child: Image.asset(YBImageStyle.getImagePath('login_password')),
                         ),
                         Container(
                           width: YBTextStyle.adapterSize8,
                         ),
                         Expanded(
                           child: TextField(
                             decoration: InputDecoration(
                               hintText: YBChineseText.mineInputYanZhengMaTitle,
                               border: InputBorder.none,
                             ),
                             controller: _editingController,
                           ),
                         ),
                         Container(
                           child: RawMaterialButton(
                               child: Text(_autoText != null? _autoText:YBChineseText.mineGetYanZhengMaTitle,style: YBTextStyle.TextStyle2D69E014Medium,),
                               onPressed: _timeCount == 60?()async{
                                 if (_phoneEditingController.text == null){
                                    YBToast.showToastInfo(YBChineseText.mineTelePhoneNoEmptyTitle);
                                    return;
                                 }
                                 _senderTimer();
                               }:null),
                         ),
                       ],
                     ),
                   ),
                   Container(
                     color: YBColor.colorF5F5F7,
                     height: 1,
                   ),
                   Container(
                     height: YBTextStyle.adapterSize30,
                   ),
                   Container(
                     height: YBTextStyle.adapterSize44,
                     child: RawMaterialButton(
                         child: Text(YBChineseText.mineEnterXiuGaiPhoneNumberTitle,style:YBTextStyle.TextStyleFFFFFF16Medium,),
                         fillColor: YBColor.commonThemeColor,
                         shape: RoundedRectangleBorder(
                           borderRadius: BorderRadius.circular(YBTextStyle.adapterSize8),
                         ),
                         onPressed:(){
                           if (_editingController.text == null){
                             YBToast.showToastInfo('验证码不能为空');
                             return;
                           }
                           _initNewPhone(_phoneEditingController.text, _editingController.text, msgTokenStr);
                         }),
                   ),
                 ],
               ),
             ),
           ),
         ),
       ),
    );
  }

  //获取验证码
  void _initLogin(){
    YBLoginInfoApi.loginInMsgCode({"phone":_phoneEditingController.text,'sysCode':"xg_youbang"},success:(data){
      print('----$data----');
      var msgTokenMap = data;
      //msgToken
      print('msgToken---$msgTokenMap');
      if (msgTokenMap['msgToken'].toString().isNotEmpty) {
        setState(() {
          msgTokenStr = msgTokenMap['msgToken'];
        });
      }
    },fail: (error){
      print("---$error---");
    });
  }

  void _initNewPhone(String customerPhone,String msgcode,String msgToken){
    YBMineNetWork.mineSettingNewPhoneChangeData({
      "customerPhone":customerPhone,
      "msgCode":msgcode,
      "msgToken":msgToken,
      "sysCode":"xg_youbang",
    },success: (data){
      bool dataBool  = data as bool;
      if (dataBool == true){
         YBToast.showToastInfo('修改成功');
         YBNavigatorUtil.goBack(context);
      }
    },fail: (error){

    });
  }

  void _senderTimer(){

    _initLogin();
    YBToast.showToastInfo("短信验证码已发送，请注意查收");
    _timer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
      setState(() {
        if (_timeCount <= 0){
          _timer.cancel();
          _timeCount = 60;
          _autoText = "重新获取";
        }else{
          _timeCount -= 1;
          _autoText = '$_timeCount' + "S";
        }
      });
    });
  }


}
